<?php

/**
 * This is the model class for table "cliente".
 *
 * The followings are the available columns in table 'cliente':
 * @property integer $idCliente
 * @property string $nombre
 * @property string $nombreContacto
 * @property string $direccion
 * @property integer $localidad
 * @property string $telefono
 * @property string $fax
 * @property string $email
 * @property string $posicion_iva
 * @property string $cuit
 * @property integer $circuito
 * @property integer $forma_pago
 * @property string $descuento
 * @property integer $plazo_envio
 * @property string $notas
 * @property integer $suspendido
 *
 * The followings are the available model relations:
 * @property Cheque[] $cheques
 * @property Localidad $localidad0
 * @property PosicionIva $posicionIva
 * @property Circuito $circuito0
 * @property Formadepago $formaPago
 * @property Percepcion $percepcion
 * @property Venta[] $ventas
 */
class Cliente extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cliente';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nombre, direccion, localidad, cuit, plazo_envio', 'required'),
			array('localidad, circuito, forma_pago, plazo_envio, suspendido', 'numerical', 'integerOnly'=>true),
			array('nombre, nombreContacto, direccion', 'length', 'max'=>100),
			array('telefono, fax', 'length', 'max'=>20),
			array('email', 'length', 'max'=>45),
			array('posicion_iva', 'length', 'max'=>1),
			array('cuit', 'length', 'max'=>11),
			array('descuento', 'length', 'max'=>10),
			array('notas', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('idCliente, nombre, nombreContacto, direccion, localidad, telefono, fax, email, posicion_iva, cuit, circuito, forma_pago, descuento, plazo_envio, notas, suspendido', 'safe', 'on'=>'search'),
		);
	}
	public function defaultScope()
	{
		return array(
				'condition'=>"suspendido=0",
		);
	}
	public function scopes(){
		return array(
				'cobranzas'=>array(
						'condition'=>'tComprobante in (1,4,9,11,3)',
						'order'=>'fecha DESC'
				),
	
		);
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'cheques' => array(self::HAS_MANY, 'Cheque', 'cliente'),
			'localidad0' => array(self::BELONGS_TO, 'Localidad', 'localidad'),
			'posicionIva' => array(self::BELONGS_TO, 'PosicionIva', 'posicion_iva'),
			'circuito0' => array(self::BELONGS_TO, 'Circuito', 'circuito'),
			'formaPago' => array(self::BELONGS_TO, 'Formadepago', 'forma_pago'),
			'percepcion' => array(self::HAS_ONE, 'Percepcion', 'cliente'),
			'ventas' => array(self::HAS_MANY, 'Venta', 'cliente'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idCliente' => 'Id Cliente',
			'nombre' => 'Nombre',
			'nombreContacto' => 'Nombre Contacto',
			'direccion' => 'Direccion',
			'localidad' => 'Localidad',
			'telefono' => 'Telefono',
			'fax' => 'Fax',
			'email' => 'Email',
			'posicion_iva' => 'Posicion Iva',
			'cuit' => 'Cuit',
			'circuito' => 'Circuito',
			'forma_pago' => 'Forma Pago',
			'descuento' => 'Descuento',
			'plazo_envio' => 'Plazo Envio',
			'notas' => 'Notas',
			'suspendido' => 'Suspendido',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		$criteria->with=array('formaPago');
		$criteria->compare('idCliente',$this->idCliente);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('nombreContacto',$this->nombreContacto,true);
		$criteria->compare('direccion',$this->direccion,true);
		$criteria->compare('localidad',$this->localidad,true);
		$criteria->compare('telefono',$this->telefono,true);
		$criteria->compare('fax',$this->fax,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('posicion_iva',$this->posicion_iva,true);
		$criteria->compare('cuit',$this->cuit,true);
		$criteria->compare('circuito',$this->circuito);
		$criteria->compare('formaPago.descripcion',$this->forma_pago,true);
		$criteria->compare('descuento',$this->descuento,true);
		$criteria->compare('plazo_envio',$this->plazo_envio);
		$criteria->compare('notas',$this->notas,true);
		$criteria->compare('suspendido',$this->suspendido);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,'sort'=>array(
					'defaultOrder'=>'t0_c1 ASC',
			)
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Cliente the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

}
